$(document).ready(function(){
  removeNav();
});
function resetPassword(box) {
  var cancleBtn = document.getElementsByClassName('accountCancle')[0];
  cancleBtn.onclick = function() {
    box.style.display = 'none';
  }
}

function account() {
  var accountBox = document.getElementsByClassName('accountBox')[0];
  accountBox.style.display = 'block';
  resetPassword(accountBox);
}

var userNameR = document.getElementById('usernameError');
var telNumR = document.getElementById('telnumError');
var oldpswR = document.getElementById('oldpswError');
var newpswR = document.getElementById('newpswError');
var conpswR = document.getElementById('conpswError');

function clearErrorText() {
  userNameR.style.display = 'none';
  telNumR.style.display = 'none';
  oldpswR.style.display = 'none';
  newpswR.style.display = 'none';
  conpswR.style.display = 'none';
}

$(document).ready(function() {
  var stuno = document.getElementById('sudentId'), //学号
    labname = document.getElementById('labName'), //实验室
    groupid = document.getElementById('group'); //小组
  username = document.getElementById('realName'), //姓名
    academy = document.getElementById('academy'), //学院
    sexy = document.getElementsByName('sexy') //性别
  stuclass = document.getElementById('classname'), //班级
    telno = document.getElementById('telPhone'), //电话
    labid = 0;
  //获取用户已有信息
  var currencyFileHref = hostUrl + "/account/getuserinfo";
  var el = {};
  el.username = localStorage.session;
  requestWithSession(el, currencyFileHref, function(data) {
      var userinfo = data.data;
      stuno.value = userinfo.username == null ? "" : userinfo.username;
      labid = userinfo.labid;
      groupid.value = userinfo.groupid == null ? "" : userinfo.groupid;
      username.value = userinfo.realname == null ? "" : userinfo.realname;
      sexy[0].checked = userinfo.sex == '男' ? true : sexy[1].checked = true;
      stuclass.value = userinfo.classname == null ? "" : userinfo.classname;
      telno.value = userinfo.tel == null ? "" : userinfo.tel;
      academy.value = userinfo.academy == null ? "" : userinfo.academy;
    },
    function(error) {
      console.log(error);
    }, true);
    //根据人员id获取
  currencyFileHref = hostUrl + "/lab/getlabtype";
  request(null, currencyFileHref, function(data) {
      var labtype = data.data;
      for (var i = 0; i < labtype.length; i++) {
        if (labid == labtype[i].labid) {
          labname.value = labtype[i].labname
        }
      }
    },
    function(error) {
      console.log(error);
    });
    //根据小组id获取小组名
    var getGroupNameUrl = hostUrl + '/group/get';
    var el = {};
    requestWithSession(el, getGroupNameUrl, function(data) {
      var groupList = data.data;
      for(var i = 0;i<groupList.length;i++){
        if(groupList[i].groupid == groupid.value){
          groupid.value = groupList[i].groupname;
        }
      }
      },
      function(error) {
        console.log(error);
      });
  stuno.style.disabled = 'true';
  groupid.style.disabled = 'true';
  labname.style.disabled = 'true';
  /*个人信息校验*/
  function checkInput(username, telnumber) {
    var userNameReg = /^([\u4E00-\u9FA5]{2,4})$/; /*二到四个汉字*/
    if (!userNameReg.test(username)) {
      userNameR.innerHTML = "姓名不合法，请重新输入！";
      userNameR.style.display = 'inline';
      return;
    } else {
      userNameR.style.display = 'none';
    }
    var telNumReg = /^(1[3578]\d{9})$/; /*号码验证*/
    if (!telNumReg.test(telnumber)) {
      telNumR.innerHTML = "电话不合法，请重新输入！";
      telNumR.style.display = 'inline';
      return;
    } else {
      telNumR.style.display = 'none';
    }
  }

  /*修改密码校验*/
  function checkAccountBox(oldpsw, newpsw, conpsw) {
    //旧密码判断oldpswR.style.display = 'none';
    var obj = {
      username: localStorage.session,
      password: oldpsw
    };
    request(obj, 'account/login', function(data) {
        var loginStatus = data.status;
        if (loginStatus === 0) {
          localStorage.session = data.data.session;
          //window.location.href = window.location.href + "homepage.html";
        } else {
          if (loginStatus === 1) {
            oldpswR.value = '账号尚未注册';
          } else if (loginStatus === 2) {
            oldpswR.value = '账号密码错误';
          } else {
            oldpswR.value = '账号禁止登录';
          }
          oldpswR.style.display = 'block';
        }
      },
      function(error) {
        alert(error.responseText);
      });
    var newpswReg = /^([a-zA-Z0-9]{6,16})$/;
    if (!newpswReg.test(newpsw)) {
      newpswR.innerHTML = "密码格式不正确，请重新输入！";
      newpswR.style.display = 'block';
      return;
    } else {
      newpswR.style.display = 'none';
    }
    if (newpsw !== conpsw) {
      conpswR.innerHTML = "两次输入密码不一致，请检查后输入！";
      conpswR.style.display = 'block';
      return;
    } else {
      conpswR.style.display = 'none';
    }
  }

  /*确认提交事件*/
  document.getElementsByClassName('applyBtn')[0]
    .onclick = function submitBtn() {
      var usernamevalue = stuno.value, //学号
        labvalue = labname.value, //实验室编号
        groupvalue = groupid.value, //小组
        uservalue = username.value, //姓名
        sexy = document.getElementsByName('sexy')[0].checked == true ?
        document.getElementsByName('sexy')[0].value : document.getElementsByName('sexy')[1].value, //性别
        academyvalue = academy.value //学院
      gradevalue = document.getElementById('gradeList').value, //年级
        stuclassvalue = stuclass.value, //班级
        telphone = telno.value; //电话

      checkInput(uservalue, telphone);

      /*向后台发出请求*/
      var currencyFileHref = hostUrl + "/account/updateinfo";
      var el = {};
      el.username = localStorage.session;
      el.realname = uservalue;
      el.sex = sexy;
      el.grade = gradevalue;
      el.academy = academyvalue;
      el.classname = stuclassvalue;
      el.labid = labid;
      el.groupid = groupvalue;
      el.tel = telphone;
      requestWithSession(el, currencyFileHref, function(data) {},
        function(error) {
          console.log(error);
        });
    }

  /*修改密码确认*/
  document.getElementsByClassName('accountConfirmBtn')[0]
    .onclick = function ConfirmBtn() {
      var oldpswvalue = document.getElementById('password').value, //旧密码
        newpswvalue = document.getElementById('newpassword').value, //新密码
        conpswvalue = document.getElementById('confirmpassword').value; //确认密码
      /* 检验密码是否合法 */
      checkAccountBox(oldpswvalue, newpswvalue, conpswvalue);

      /*向后台发出请求*/
      var reqUrl = hostUrl + '/account/changepsw';
      var el = {};
      el.username = localStorage.session;
      el.password = newpswvalue;
      requestWithSession(el, reqUrl, function(data) {},
        function(error) {
          console.log(error);
        });
        alert('修改成功！');
        document.getElementsByClassName('accountBox')[0].style.display = 'none';
    }
});
